package com.okaysoft.ofb.account

import org.springframework.dao.DataIntegrityViolationException
import grails.converters.JSON

class RequestmapController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
    }

    def listJson() {
        params.max = Math.min(params.rows ? params.int('rows') : 10, 100)
        params.page = params.page ? params.int('page') : 0
        params.offset = (params.page - 1) * params.max
        // params.sort = "orderNumber"
        // params.order = "asc"

        def requestmaps = Requestmap.list(params)
        requestmaps = requestmaps.collect {rm ->
            [id: rm.id, name: rm.name, link: rm.link, url: rm.url, configAttribute: rm.configAttribute, orderNumber: rm.orderNumber, menuEnabled: rm.menuEnabled,parentName: rm.parentName,parentId: rm.parentId]
        }
        def count = Requestmap.count()
        render([total: count, rows: requestmaps] as JSON)
    }
    
    def listMenu() {
        def requestmaps = Requestmap.findAllByMenuEnabled(Boolean.TRUE)
        requestmaps = requestmaps.collect {rm ->
            [id: rm.id, text: rm.name, link: rm.link, url: rm.url, configAttribute: rm.configAttribute, orderNumber: rm.orderNumber, menuEnabled: rm.menuEnabled,parentName: rm.parentName,parentId: rm.parentId]
        }
        render(requestmaps as JSON)
    }

    def create() {
        [requestmapInstance: new Requestmap(params)]
    }

    def save() {
        def requestmapInstance = Requestmap.get(params.id)
        if (!requestmapInstance) {
            requestmapInstance = new Requestmap(params)
        }

        if (params.configAttribute && !params.configAttribute instanceof String) {
            def cas
            params.configAttribute.each {
                if (!cas) {
                    cas = "${it}"
                } else {
                    cas += ",${it}"
                }
            }
            params.configAttribute = cas
        }

        requestmapInstance.properties = params

        if (!requestmapInstance.save(flush: true)) {
            def failure =  [status: 0, msg: message(code: 'default.save.failure.message', default: 'Save failure')] as JSON
            render(failure.toString())
            return
        }
        def succ = [status: 1, msg: message(code: 'default.save.success.message', default: 'Save success')] as JSON
        render(succ.toString())
    }

    def show() {
        def requestmapInstance = Requestmap.get(params.id)
        if (!requestmapInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'requestmap.label', default: 'Requestmap'), params.id])
            redirect(action: "list")
            return
        }

        [requestmapInstance: requestmapInstance]
    }

    def delete() {
        def requestmapInstance = Requestmap.get(params.id)
        if (!requestmapInstance) {
            render([status: '0',msg:  message(code: 'default.not.found.message', args: [message(code: 'requestmap.label', default: 'Requestmap'), params.id])] as JSON)
            return
        }

        try {
            requestmapInstance.delete(flush: true)
            render([status: '1',msg:message(code: 'default.deleted.message', args: [message(code: 'requestmap.label', default: 'Requestmap'), params.id])] as JSON)
            return
        }
        catch (DataIntegrityViolationException e) {
            render([status: '0',msg:message(code: 'default.not.deleted.message', args: [message(code: 'requestmap.label', default: 'Requestmap'), params.id])] as JSON)
            return
        }
    }
}
